home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / programs_-_include / NET / SOCK.H < prev    next >
C/C++ Source or Header  |  1999-09-17  |  27KB  |  947 lines

  1. /*
  2.  * INET        An implementation of the TCP/IP protocol suite for the LINUX
  3.  *        operating system.  INET is implemented using the  BSD Socket
  4.  *        interface as the means of communication with the user level.
  5.  *
  6.  *        Definitions for the AF_INET socket handler.
  7.  *
  8.  * Version:    @(#)sock.h    1.0.4    05/13/93
  9.  *
  10.  * Authors:    Ross Biro, <bir7@leland.Stanford.Edu>
  11.  *        Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12.  *        Corey Minyard <wf-rch!minyard@relay.EU.net>
  13.  *        Florian La Roche <flla@stud.uni-sb.de>
  14.  *
  15.  * Fixes:
  16.  *        Alan Cox    :    Volatiles in skbuff pointers. See
  17.  *                    skbuff comments. May be overdone,
  18.  *                    better to prove they can be removed
  19.  *                    than the reverse.
  20.  *        Alan Cox    :    Added a zapped field for tcp to note
  21.  *                    a socket is reset and must stay shut up
  22.  *        Alan Cox    :    New fields for options
  23.  *    Pauline Middelink    :    identd support
  24.  *        Alan Cox    :    Eliminate low level recv/recvfrom
  25.  *        David S. Miller    :    New socket lookup architecture.
  26.  *              Steve Whitehouse:       Default routines for sock_ops
  27.  *
  28.  *        This program is free software; you can redistribute it and/or
  29.  *        modify it under the terms of the GNU General Public License
  30.  *        as published by the Free Software Foundation; either version
  31.  *        2 of the License, or (at your option) any later version.
  32.  */
  33. #ifndef _SOCK_H
  34. #define _SOCK_H
  35.  
  36. #include <linux/config.h>
  37. #include <linux/timer.h>
  38. #include <linux/in.h>        /* struct sockaddr_in */
  39.  
  40. #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
  41. #include <linux/in6.h>        /* struct sockaddr_in6 */
  42. #include <linux/ipv6.h>        /* dest_cache, inet6_options */
  43. #include <linux/icmpv6.h>
  44. #include <net/if_inet6.h>    /* struct ipv6_mc_socklist */
  45. #endif
  46.  
  47. #if defined(CONFIG_INET) || defined (CONFIG_INET_MODULE)
  48. #include <linux/icmp.h>
  49. #endif
  50. #include <linux/tcp.h>        /* struct tcphdr */
  51.  
  52. #include <linux/netdevice.h>
  53. #include <linux/skbuff.h>    /* struct sk_buff */
  54. #include <net/protocol.h>        /* struct inet_protocol */
  55. #if defined(CONFIG_X25) || defined(CONFIG_X25_MODULE)
  56. #include <net/x25.h>
  57. #endif
  58. #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
  59. #include <net/ax25.h>
  60. #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
  61. #include <net/netrom.h>
  62. #endif
  63. #if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE)
  64. #include <net/rose.h>
  65. #endif
  66. #endif
  67.  
  68. #if defined(CONFIG_IPX) || defined(CONFIG_IPX_MODULE)
  69. #if defined(CONFIG_SPX) || defined(CONFIG_SPX_MODULE)
  70. #include <net/spx.h>
  71. #else
  72. #include <net/ipx.h>
  73. #endif /* CONFIG_SPX */
  74. #endif /* CONFIG_IPX */
  75.  
  76. #if defined(CONFIG_ATALK) || defined(CONFIG_ATALK_MODULE)
  77. #include <linux/atalk.h>
  78. #endif
  79.  
  80. #if defined(CONFIG_DECNET) || defined(CONFIG_DECNET_MODULE)
  81. #include <net/dn.h>
  82. #endif
  83.  
  84. #if defined(CONFIG_IRDA) || defined(CONFIG_IRDA_MODULE)
  85. #include <net/irda/irda.h>
  86. #endif
  87.  
  88. #ifdef CONFIG_FILTER
  89. #include <linux/filter.h>
  90. #endif
  91.  
  92. #include <asm/atomic.h>
  93.  
  94. #define MIN_WRITE_SPACE    2048
  95.  
  96. /* The AF_UNIX specific socket options */
  97. struct unix_opt {
  98.     int             family;
  99.     char *            name;
  100.     int              locks;
  101.     struct unix_address    *addr;
  102.     struct dentry *        dentry;
  103.     struct semaphore    readsem;
  104.     struct sock *        other;
  105.     struct sock **        list;
  106.     struct sock *        gc_tree;
  107.     int            inflight;
  108. };
  109.  
  110. #ifdef CONFIG_NETLINK
  111. struct netlink_callback;
  112.  
  113. struct netlink_opt {
  114.     pid_t            pid;
  115.     unsigned        groups;
  116.     pid_t            dst_pid;
  117.     unsigned        dst_groups;
  118.     int            (*handler)(int unit, struct sk_buff *skb);
  119.     atomic_t        locks;
  120.     struct netlink_callback    *cb;
  121. };
  122. #endif
  123.  
  124. /* Once the IPX ncpd patches are in these are going into protinfo. */
  125. #if defined(CONFIG_IPX) || defined(CONFIG_IPX_MODULE)
  126. struct ipx_opt {
  127.     ipx_address        dest_addr;
  128.     ipx_interface        *intrfc;
  129.     unsigned short        port;
  130. #ifdef CONFIG_IPX_INTERN
  131.     unsigned char           node[IPX_NODE_LEN];
  132. #endif
  133.     unsigned short        type;
  134. /* 
  135.  * To handle special ncp connection-handling sockets for mars_nwe,
  136.  * the connection number must be stored in the socket.
  137.  */
  138.     unsigned short        ipx_ncp_conn;
  139. };
  140. #endif
  141.  
  142. #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
  143. struct ipv6_pinfo {
  144.     struct in6_addr     saddr;
  145.     struct in6_addr     rcv_saddr;
  146.     struct in6_addr        daddr;
  147.     struct in6_addr        *daddr_cache;
  148.  
  149.     __u32            flow_lbl;
  150.     __u32            frag_size;
  151.     int            hop_limit;
  152.     int            mcast_hops;
  153.     int            mcast_oif;
  154.     __u8            priority;
  155.  
  156.     /* pktoption flags */
  157.     union {
  158.         struct {
  159.             __u8    srcrt:2,
  160.                     rxinfo:1,
  161.                 rxhlim:1,
  162.                 hopopts:1,
  163.                 dstopts:1,
  164.                                 authhdr:1,
  165.                                 unused:1;
  166.         } bits;
  167.         __u8        all;
  168.     } rxopt;
  169.  
  170.     /* sockopt flags */
  171.     __u8            mc_loop:1,
  172.                             recverr:1,
  173.                             pmtudisc:2;
  174.  
  175.     struct ipv6_mc_socklist    *ipv6_mc_list;
  176.     __u32            dst_cookie;
  177.  
  178.     struct ipv6_txoptions    *opt;
  179.     struct sk_buff        *pktoptions;
  180. };
  181.  
  182. struct raw6_opt {
  183.     __u32            checksum;    /* perform checksum */
  184.     __u32            offset;        /* checksum offset  */
  185.  
  186.     struct icmp6_filter    filter;
  187. };
  188.  
  189. #endif /* IPV6 */
  190.  
  191. #if defined(CONFIG_INET) || defined(CONFIG_INET_MODULE)
  192. struct raw_opt {
  193.     struct icmp_filter    filter;
  194. };
  195. #endif
  196.  
  197. /* This defines a selective acknowledgement block. */
  198. struct tcp_sack_block {
  199.     __u32    start_seq;
  200.     __u32    end_seq;
  201. };
  202.  
  203. struct tcp_opt {
  204.     int    tcp_header_len;    /* Bytes of tcp header to send        */
  205.  
  206. /*
  207.  *    Header prediction flags
  208.  *    0x5?10 << 16 + snd_wnd in net byte order
  209.  */
  210.     __u32    pred_flags;
  211.  
  212. /*
  213.  *    RFC793 variables by their proper names. This means you can
  214.  *    read the code and the spec side by side (and laugh ...)
  215.  *    See RFC793 and RFC1122. The RFC writes these in capitals.
  216.  */
  217.      __u32    rcv_nxt;    /* What we want to receive next     */
  218.      __u32    snd_nxt;    /* Next sequence we send        */
  219.  
  220.      __u32    snd_una;    /* First byte we want an ack for    */
  221.     __u32    rcv_tstamp;    /* timestamp of last received packet    */
  222.     __u32    lrcvtime;    /* timestamp of last received data packet*/
  223.     __u32    srtt;        /* smothed round trip time << 3        */
  224.  
  225.     __u32    ato;        /* delayed ack timeout            */
  226.     __u32    snd_wl1;    /* Sequence for window update        */
  227.  
  228.     __u32    snd_wl2;    /* Ack sequence for update        */
  229.     __u32    snd_wnd;    /* The window we expect to receive    */
  230.     __u32    max_window;
  231.     __u32    pmtu_cookie;    /* Last pmtu seen by socket        */
  232.     __u16    mss_cache;    /* Cached effective mss, not including SACKS */
  233.     __u16    mss_clamp;    /* Maximal mss, negotiated at connection setup */
  234.     __u16    ext_header_len;    /* Dave, do you allow mw to use this hole? 8) --ANK */
  235.     __u8    pending;    /* pending events            */
  236.     __u8    retransmits;
  237.     __u32    last_ack_sent;    /* last ack we sent            */
  238.  
  239.     __u32    backoff;    /* backoff                */
  240.     __u32    mdev;        /* medium deviation            */
  241.      __u32    snd_cwnd;    /* Sending congestion window        */
  242.     __u32    rto;        /* retransmit timeout            */
  243.  
  244.     __u32    packets_out;    /* Packets which are "in flight"    */
  245.     __u32    fackets_out;    /* Non-retrans SACK'd packets        */
  246.     __u32    retrans_out;    /* Fast-retransmitted packets out    */
  247.     __u32    high_seq;    /* snd_nxt at onset of congestion    */
  248. /*
  249.  *    Slow start and congestion control (see also Nagle, and Karn & Partridge)
  250.  */
  251.      __u32    snd_ssthresh;    /* Slow start size threshold        */
  252.      __u16    snd_cwnd_cnt;    /* Linear increase counter        */
  253.     __u8    dup_acks;    /* Consequetive duplicate acks seen from other end */
  254.     __u8    delayed_acks;
  255.     __u16    user_mss;      /* mss requested by user in ioctl */
  256.  
  257.     /* Two commonly used timers in both sender and receiver paths. */
  258.      struct timer_list    retransmit_timer;    /* Resend (no ack)    */
  259.      struct timer_list    delack_timer;        /* Ack delay         */
  260.  
  261.     struct sk_buff_head    out_of_order_queue; /* Out of order segments go here */
  262.  
  263.     struct tcp_func        *af_specific;    /* Operations which are AF_INET{4,6} specific    */
  264.     struct sk_buff        *send_head;    /* Front of stuff to transmit            */
  265.     struct sk_buff        *retrans_head;    /* retrans head can be 
  266.                          * different to the head of
  267.                          * write queue if we are doing
  268.                          * fast retransmit
  269.                          */
  270.  
  271.      __u32    rcv_wnd;    /* Current receiver window        */
  272.     __u32    rcv_wup;    /* rcv_nxt on last window update sent    */
  273.     __u32    write_seq;
  274.     __u32    copied_seq;
  275. /*
  276.  *      Options received (usually on last packet, some only on SYN packets).
  277.  */
  278.     char    tstamp_ok,    /* TIMESTAMP seen on SYN packet        */
  279.         wscale_ok,    /* Wscale seen on SYN packet        */
  280.         sack_ok;    /* SACK seen on SYN packet        */
  281.     char    saw_tstamp;    /* Saw TIMESTAMP on last packet        */
  282.         __u8    snd_wscale;    /* Window scaling received from sender    */
  283.         __u8    rcv_wscale;    /* Window scaling to send to receiver    */
  284.     __u8    rexmt_done;    /* Retransmitted up to send head?    */
  285.         __u32    rcv_tsval;    /* Time stamp value                 */
  286.         __u32    rcv_tsecr;    /* Time stamp echo reply            */
  287.         __u32    ts_recent;    /* Time stamp to echo next        */
  288.         __u32    ts_recent_stamp;/* Time we stored ts_recent (for aging) */
  289.     int    num_sacks;    /* Number of SACK blocks        */
  290.     struct tcp_sack_block selective_acks[4]; /* The SACKS themselves*/
  291.  
  292.      struct timer_list    probe_timer;        /* Probes    */
  293.     __u32    window_clamp;    /* XXX Document this... -DaveM        */
  294.     __u32    probes_out;    /* unanswered 0 window probes        */
  295.     __u32    syn_seq;
  296.     __u32    fin_seq;
  297.     __u32    urg_seq;
  298.     __u32    urg_data;
  299.  
  300.     __u32    last_seg_size;    /* Size of last incoming segment */
  301.     __u32    rcv_mss;    /* MSS used for delayed ACK decisions */ 
  302.  
  303.     struct open_request    *syn_wait_queue;
  304.     struct open_request    **syn_wait_last;
  305.  
  306.     int syn_backlog;    /* Backlog of received SYNs */
  307. };
  308.  
  309.      
  310. /*
  311.  * This structure really needs to be cleaned up.
  312.  * Most of it is for TCP, and not used by any of
  313.  * the other protocols.
  314.  */
  315.  
  316. /*
  317.  * The idea is to start moving to a newer struct gradualy
  318.  * 
  319.  * IMHO the newer struct should have the following format:
  320.  * 
  321.  *    struct sock {
  322.  *        sockmem [mem, proto, callbacks]
  323.  *
  324.  *        union or struct {
  325.  *            ax25;
  326.  *        } ll_pinfo;
  327.  *    
  328.  *        union {
  329.  *            ipv4;
  330.  *            ipv6;
  331.  *            ipx;
  332.  *            netrom;
  333.  *            rose;
  334.  *             x25;
  335.  *        } net_pinfo;
  336.  *
  337.  *        union {
  338.  *            tcp;
  339.  *            udp;
  340.  *            spx;
  341.  *            netrom;
  342.  *        } tp_pinfo;
  343.  *
  344.  *    }
  345.  */
  346.  
  347. /* Define this to get the sk->debug debugging facility. */
  348. #define SOCK_DEBUGGING
  349. #ifdef SOCK_DEBUGGING
  350. #define SOCK_DEBUG(sk, msg...) do { if((sk) && ((sk)->debug)) printk(KERN_DEBUG ## msg); } while (0)
  351. #else
  352. #define SOCK_DEBUG(sk, msg...) do { } while (0)
  353. #endif
  354.  
  355. struct sock {
  356.     /* This must be first. */
  357.     struct sock        *sklist_next;
  358.     struct sock        *sklist_prev;
  359.  
  360.     /* Local port binding hash linkage. */
  361.     struct sock        *bind_next;
  362.     struct sock        **bind_pprev;
  363.  
  364.     /* Socket demultiplex comparisons on incoming packets. */
  365.     __u32            daddr;        /* Foreign IPv4 addr            */
  366.     __u32            rcv_saddr;    /* Bound local IPv4 addr        */
  367.     __u16            dport;        /* Destination port            */
  368.     unsigned short        num;        /* Local port                */
  369.     int            bound_dev_if;    /* Bound device index if != 0        */
  370.  
  371.     /* Main hash linkage for various protocol lookup tables. */
  372.     struct sock        *next;
  373.     struct sock        **pprev;
  374.  
  375.     volatile unsigned char    state,        /* Connection state            */
  376.                 zapped;        /* In ax25 & ipx means not linked    */
  377.     __u16            sport;        /* Source port                */
  378.  
  379.     unsigned short        family;        /* Address family            */
  380.     unsigned char        reuse,        /* SO_REUSEADDR setting            */
  381.                 nonagle;    /* Disable Nagle algorithm?        */
  382.  
  383.     atomic_t        sock_readers;    /* User count                */
  384.     int            rcvbuf;        /* Size of receive buffer in bytes    */
  385.  
  386.     struct wait_queue    **sleep;    /* Sock wait queue            */
  387.     struct dst_entry    *dst_cache;    /* Destination cache            */
  388.     atomic_t        rmem_alloc;    /* Receive queue bytes committed    */
  389.     struct sk_buff_head    receive_queue;    /* Incoming packets            */
  390.     atomic_t        wmem_alloc;    /* Transmit queue bytes committed    */
  391.     struct sk_buff_head    write_queue;    /* Packet sending queue            */
  392.     atomic_t        omem_alloc;    /* "o" is "option" or "other" */
  393.     __u32            saddr;        /* Sending source            */
  394.     unsigned int        allocation;    /* Allocation mode            */
  395.     int            sndbuf;        /* Size of send buffer in bytes        */
  396.     struct sock        *prev;
  397.  
  398.     /* Not all are volatile, but some are, so we might as well say they all are.
  399.      * XXX Make this a flag word -DaveM
  400.      */
  401.     volatile char        dead,
  402.                 done,
  403.                 urginline,
  404.                 keepopen,
  405.                 linger,
  406.                 destroy,
  407.                 no_check,
  408.                 broadcast,
  409.                 bsdism;
  410.     unsigned char        debug;
  411.     int            proc;
  412.     unsigned long            lingertime;
  413.  
  414.     int            hashent;
  415.     struct sock        *pair;
  416.  
  417.     /* Error and backlog packet queues, rarely used. */
  418.     struct sk_buff_head    back_log,
  419.                             error_queue;
  420.  
  421.     struct proto        *prot;
  422.  
  423.     unsigned short        shutdown;
  424.  
  425. #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
  426.     union {
  427.         struct ipv6_pinfo    af_inet6;
  428.     } net_pinfo;
  429. #endif
  430.  
  431.     union {
  432.         struct tcp_opt        af_tcp;
  433. #if defined(CONFIG_INET) || defined (CONFIG_INET_MODULE)
  434.         struct raw_opt        tp_raw4;
  435. #endif
  436. #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
  437.         struct raw6_opt        tp_raw;
  438. #endif /* CONFIG_IPV6 */
  439. #if defined(CONFIG_SPX) || defined (CONFIG_SPX_MODULE)
  440.         struct spx_opt        af_spx;
  441. #endif /* CONFIG_SPX */
  442.  
  443.     } tp_pinfo;
  444.  
  445.     int            err, err_soft;    /* Soft holds errors that don't
  446.                            cause failure but are the cause
  447.                            of a persistent failure not just
  448.                            'timed out' */
  449.     unsigned short        ack_backlog;
  450.     unsigned short        max_ack_backlog;
  451.     __u32            priority;
  452.     unsigned short        type;
  453.     unsigned char        localroute;    /* Route locally only */
  454.     unsigned char        protocol;
  455.     struct ucred        peercred;
  456.  
  457. #ifdef CONFIG_FILTER
  458.     /* Socket Filtering Instructions */
  459.     struct sk_filter          *filter;
  460. #endif /* CONFIG_FILTER */
  461.  
  462.     /* This is where all the private (optional) areas that don't
  463.      * overlap will eventually live. 
  464.      */
  465.     union {
  466.         void *destruct_hook;
  467.           struct unix_opt    af_unix;
  468. #if defined(CONFIG_ATALK) || defined(CONFIG_ATALK_MODULE)
  469.         struct atalk_sock    af_at;
  470. #endif
  471. #if defined(CONFIG_IPX) || defined(CONFIG_IPX_MODULE)
  472.         struct ipx_opt        af_ipx;
  473. #endif
  474. #if defined (CONFIG_DECNET) || defined(CONFIG_DECNET_MODULE)
  475.         struct dn_scp           dn;
  476. #endif
  477. #if defined (CONFIG_PACKET) || defined(CONFIG_PACKET_MODULE)
  478.         struct packet_opt    *af_packet;
  479. #endif
  480. #if defined(CONFIG_X25) || defined(CONFIG_X25_MODULE)
  481.         x25_cb            *x25;
  482. #endif
  483. #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
  484.         ax25_cb            *ax25;
  485. #endif
  486. #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
  487.         nr_cb            *nr;
  488. #endif
  489. #if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE)
  490.         rose_cb            *rose;
  491. #endif
  492. #ifdef CONFIG_NETLINK
  493.         struct netlink_opt    af_netlink;
  494. #endif
  495. #if defined(CONFIG_ECONET) || defined(CONFIG_ECONET_MODULE)
  496.         struct econet_opt    *af_econet;
  497. #endif
  498. #if defined(CONFIG_IRDA) || defined(CONFIG_IRDA_MODULE)
  499.         struct irda_sock        *irda;
  500. #endif
  501.     } protinfo;          
  502.  
  503.     /* IP 'private area' or will be eventually. */
  504.     int            ip_ttl;            /* TTL setting */
  505.     int            ip_tos;            /* TOS */
  506.     unsigned           ip_cmsg_flags;
  507.     struct ip_options    *opt;
  508.     unsigned char        ip_hdrincl;        /* Include headers ? */
  509.     __u8            ip_mc_ttl;        /* Multicasting TTL */
  510.     __u8            ip_mc_loop;        /* Loopback */
  511.     __u8            ip_recverr;
  512.     __u8            ip_pmtudisc;
  513.     int            ip_mc_index;        /* Multicast device index */
  514.     __u32            ip_mc_addr;
  515.     struct ip_mc_socklist    *ip_mc_list;        /* Group array */
  516.  
  517.     /* This part is used for the timeout functions (timer.c). */
  518.     int            timeout;    /* What are we waiting for? */
  519.     struct timer_list    timer;        /* This is the sock cleanup timer. */
  520.     struct timeval        stamp;
  521.  
  522.     /* Identd */
  523.     struct socket        *socket;
  524.  
  525.     /* RPC layer private data */
  526.     void            *user_data;
  527.   
  528.     /* Callbacks */
  529.     void            (*state_change)(struct sock *sk);
  530.     void            (*data_ready)(struct sock *sk,int bytes);
  531.     void            (*write_space)(struct sock *sk);
  532.     void            (*error_report)(struct sock *sk);
  533.  
  534.       int            (*backlog_rcv) (struct sock *sk,
  535.                         struct sk_buff *skb);  
  536.     void                    (*destruct)(struct sock *sk);
  537. };
  538.  
  539. /* IP protocol blocks we attach to sockets.
  540.  * socket layer -> transport layer interface
  541.  * transport -> network interface is defined by struct inet_proto
  542.  */
  543. struct proto {
  544.     /* These must be first. */
  545.     struct sock        *sklist_next;
  546.     struct sock        *sklist_prev;
  547.  
  548.     void            (*close)(struct sock *sk, 
  549.                     long timeout);
  550.     int            (*connect)(struct sock *sk,
  551.                         struct sockaddr *uaddr, 
  552.                     int addr_len);
  553.  
  554.     struct sock *        (*accept) (struct sock *sk, int flags);
  555.     void            (*retransmit)(struct sock *sk, int all);
  556.     void            (*write_wakeup)(struct sock *sk);
  557.     void            (*read_wakeup)(struct sock *sk);
  558.  
  559.     unsigned int        (*poll)(struct file * file, struct socket *sock,
  560.                     struct poll_table_struct *wait);
  561.  
  562.     int            (*ioctl)(struct sock *sk, int cmd,
  563.                      unsigned long arg);
  564.     int            (*init)(struct sock *sk);
  565.     int            (*destroy)(struct sock *sk);
  566.     void            (*shutdown)(struct sock *sk, int how);
  567.     int            (*setsockopt)(struct sock *sk, int level, 
  568.                     int optname, char *optval, int optlen);
  569.     int            (*getsockopt)(struct sock *sk, int level, 
  570.                     int optname, char *optval, 
  571.                     int *option);       
  572.     int            (*sendmsg)(struct sock *sk, struct msghdr *msg,
  573.                        int len);
  574.     int            (*recvmsg)(struct sock *sk, struct msghdr *msg,
  575.                     int len, int noblock, int flags, 
  576.                     int *addr_len);
  577.     int            (*bind)(struct sock *sk, 
  578.                     struct sockaddr *uaddr, int addr_len);
  579.  
  580.     int            (*backlog_rcv) (struct sock *sk, 
  581.                         struct sk_buff *skb);
  582.  
  583.     /* Keeping track of sk's, looking them up, and port selection methods. */
  584.     void            (*hash)(struct sock *sk);
  585.     void            (*unhash)(struct sock *sk);
  586.     void            (*rehash)(struct sock *sk);
  587.     unsigned short        (*good_socknum)(void);
  588.     int            (*verify_bind)(struct sock *sk, unsigned short snum);
  589.  
  590.     unsigned short        max_header;
  591.     unsigned long        retransmits;
  592.     char            name[32];
  593.     int            inuse, highestinuse;
  594. };
  595.  
  596. #define TIME_WRITE    1    /* Not yet used */
  597. #define TIME_RETRANS    2    /* Retransmit timer */
  598. #define TIME_DACK    3    /* Delayed ack timer */
  599. #define TIME_CLOSE    4
  600. #define TIME_KEEPOPEN    5
  601. #define TIME_DESTROY    6
  602. #define TIME_DONE    7    /* Used to absorb those last few packets */
  603. #define TIME_PROBE0    8
  604.  
  605. /* About 10 seconds */
  606. #define SOCK_DESTROY_TIME (10*HZ)
  607.  
  608. /* Sockets 0-1023 can't be bound to unless you are superuser */
  609. #define PROT_SOCK    1024
  610.  
  611. #define SHUTDOWN_MASK    3
  612. #define RCV_SHUTDOWN    1
  613. #define SEND_SHUTDOWN    2
  614.  
  615. /* Per-protocol hash table implementations use this to make sure
  616.  * nothing changes.
  617.  */
  618. #define SOCKHASH_LOCK()        start_bh_atomic()
  619. #define SOCKHASH_UNLOCK()    end_bh_atomic()
  620.  
  621. /* Some things in the kernel just want to get at a protocols
  622.  * entire socket list commensurate, thus...
  623.  */
  624. static __inline__ void add_to_prot_sklist(struct sock *sk)
  625. {
  626.     SOCKHASH_LOCK();
  627.     if(!sk->sklist_next) {
  628.         struct proto *p = sk->prot;
  629.  
  630.         sk->sklist_prev = (struct sock *) p;
  631.         sk->sklist_next = p->sklist_next;
  632.         p->sklist_next->sklist_prev = sk;
  633.         p->sklist_next = sk;
  634.  
  635.         /* Charge the protocol. */
  636.         sk->prot->inuse += 1;
  637.         if(sk->prot->highestinuse < sk->prot->inuse)
  638.             sk->prot->highestinuse = sk->prot->inuse;
  639.     }
  640.     SOCKHASH_UNLOCK();
  641. }
  642.  
  643. static __inline__ void del_from_prot_sklist(struct sock *sk)
  644. {
  645.     SOCKHASH_LOCK();
  646.     if(sk->sklist_next) {
  647.         sk->sklist_next->sklist_prev = sk->sklist_prev;
  648.         sk->sklist_prev->sklist_next = sk->sklist_next;
  649.         sk->sklist_next = NULL;
  650.         sk->prot->inuse--;
  651.     }
  652.     SOCKHASH_UNLOCK();
  653. }
  654.  
  655. /*
  656.  * Used by processes to "lock" a socket state, so that
  657.  * interrupts and bottom half handlers won't change it
  658.  * from under us. It essentially blocks any incoming
  659.  * packets, so that we won't get any new data or any
  660.  * packets that change the state of the socket.
  661.  *
  662.  * Note the 'barrier()' calls: gcc may not move a lock
  663.  * "downwards" or a unlock "upwards" when optimizing.
  664.  */
  665. extern void __release_sock(struct sock *sk);
  666.  
  667. static inline void lock_sock(struct sock *sk)
  668. {
  669. #if 0
  670. /* debugging code: the test isn't even 100% correct, but it can catch bugs */
  671. /* Note that a double lock is ok in theory - it's just _usually_ a bug */
  672.     if (atomic_read(&sk->sock_readers)) {
  673.         __label__ here;
  674.         printk("double lock on socket at %p\n", &&here);
  675. here:
  676.     }
  677. #endif
  678.     atomic_inc(&sk->sock_readers);
  679.     synchronize_bh();
  680. }
  681.  
  682. static inline void release_sock(struct sock *sk)
  683. {
  684.     barrier();
  685.     if (atomic_dec_and_test(&sk->sock_readers))
  686.         __release_sock(sk);
  687. }
  688.  
  689. /*
  690.  *    This might not be the most appropriate place for this two     
  691.  *    but since they are used by a lot of the net related code
  692.  *    at least they get declared on a include that is common to all
  693.  */
  694.  
  695. static __inline__ int min(unsigned int a, unsigned int b)
  696. {
  697.     if (a > b)
  698.         a = b; 
  699.     return a;
  700. }
  701.  
  702. static __inline__ int max(unsigned int a, unsigned int b)
  703. {
  704.     if (a < b)
  705.         a = b;
  706.     return a;
  707. }
  708.  
  709. extern struct sock *        sk_alloc(int family, int priority, int zero_it);
  710. extern void            sk_free(struct sock *sk);
  711. extern void            destroy_sock(struct sock *sk);
  712.  
  713. extern struct sk_buff        *sock_wmalloc(struct sock *sk,
  714.                           unsigned long size, int force,
  715.                           int priority);
  716. extern struct sk_buff        *sock_rmalloc(struct sock *sk,
  717.                           unsigned long size, int force,
  718.                           int priority);
  719. extern void            sock_wfree(struct sk_buff *skb);
  720. extern void            sock_rfree(struct sk_buff *skb);
  721. extern unsigned long        sock_rspace(struct sock *sk);
  722. extern unsigned long        sock_wspace(struct sock *sk);
  723.  
  724. extern int            sock_setsockopt(struct socket *sock, int level,
  725.                         int op, char *optval,
  726.                         int optlen);
  727.  
  728. extern int            sock_getsockopt(struct socket *sock, int level,
  729.                         int op, char *optval, 
  730.                         int *optlen);
  731. extern struct sk_buff         *sock_alloc_send_skb(struct sock *sk,
  732.                              unsigned long size,
  733.                              unsigned long fallback,
  734.                              int noblock,
  735.                              int *errcode);
  736. extern void *sock_kmalloc(struct sock *sk, int size, int priority);
  737. extern void sock_kfree_s(struct sock *sk, void *mem, int size);
  738.  
  739.  
  740. /*
  741.  * Functions to fill in entries in struct proto_ops when a protocol
  742.  * does not implement a particular function.
  743.  */
  744. extern int                      sock_no_dup(struct socket *, struct socket *);
  745. extern int                      sock_no_release(struct socket *, 
  746.                         struct socket *);
  747. extern int                      sock_no_bind(struct socket *, 
  748.                          struct sockaddr *, int);
  749. extern int                      sock_no_connect(struct socket *,
  750.                         struct sockaddr *, int, int);
  751. extern int                      sock_no_socketpair(struct socket *,
  752.                            struct socket *);
  753. extern int                      sock_no_accept(struct socket *,
  754.                            struct socket *, int);
  755. extern int                      sock_no_getname(struct socket *,
  756.                         struct sockaddr *, int *, int);
  757. extern unsigned int             sock_no_poll(struct file *, struct socket *,
  758.                          struct poll_table_struct *);
  759. extern int                      sock_no_ioctl(struct socket *, unsigned int,
  760.                           unsigned long);
  761. extern int            sock_no_listen(struct socket *, int);
  762. extern int                      sock_no_shutdown(struct socket *, int);
  763. extern int            sock_no_getsockopt(struct socket *, int , int,
  764.                            char *, int *);
  765. extern int            sock_no_setsockopt(struct socket *, int, int,
  766.                            char *, int);
  767. extern int             sock_no_fcntl(struct socket *, 
  768.                           unsigned int, unsigned long);
  769. extern int                      sock_no_sendmsg(struct socket *,
  770.                         struct msghdr *, int,
  771.                         struct scm_cookie *);
  772. extern int                      sock_no_recvmsg(struct socket *,
  773.                         struct msghdr *, int,
  774.                         struct scm_cookie *);
  775.  
  776. /*
  777.  *    Default socket callbacks and setup code
  778.  */
  779.  
  780. extern void sock_def_callback1(struct sock *);
  781. extern void sock_def_callback2(struct sock *, int);
  782. extern void sock_def_callback3(struct sock *);
  783. extern void sock_def_destruct(struct sock *);
  784.  
  785. /* Initialise core socket variables */
  786. extern void sock_init_data(struct socket *sock, struct sock *sk);
  787.  
  788. extern void sklist_remove_socket(struct sock **list, struct sock *sk);
  789. extern void sklist_insert_socket(struct sock **list, struct sock *sk);
  790. extern void sklist_destroy_socket(struct sock **list, struct sock *sk);
  791.  
  792. #ifdef CONFIG_FILTER
  793. /*
  794.  * Run the filter code and then cut skb->data to correct size returned by
  795.  * sk_run_filter. If pkt_len is 0 we toss packet. If skb->len is smaller
  796.  * than pkt_len we keep whole skb->data.
  797.  */
  798. extern __inline__ int sk_filter(struct sk_buff *skb, struct sk_filter *filter)
  799. {
  800.     int pkt_len;
  801.  
  802.         pkt_len = sk_run_filter(skb, filter->insns, filter->len);
  803.         if(!pkt_len)
  804.                 return 1;    /* Toss Packet */
  805.         else
  806.                 skb_trim(skb, pkt_len);
  807.  
  808.     return 0;
  809. }
  810.  
  811. extern __inline__ void sk_filter_release(struct sock *sk, struct sk_filter *fp)
  812. {
  813.     unsigned int size = sk_filter_len(fp);
  814.  
  815.     atomic_sub(size, &sk->omem_alloc);
  816.  
  817.     if (atomic_dec_and_test(&fp->refcnt))
  818.         kfree_s(fp, size);
  819. }
  820.  
  821. extern __inline__ void sk_filter_charge(struct sock *sk, struct sk_filter *fp)
  822. {
  823.     atomic_inc(&fp->refcnt);
  824.     atomic_add(sk_filter_len(fp), &sk->omem_alloc);
  825. }
  826.  
  827. #endif /* CONFIG_FILTER */
  828.  
  829. /*
  830.  *     Queue a received datagram if it will fit. Stream and sequenced
  831.  *    protocols can't normally use this as they need to fit buffers in
  832.  *    and play with them.
  833.  *
  834.  *     Inlined as it's very short and called for pretty much every
  835.  *    packet ever received.
  836.  */
  837.  
  838. extern __inline__ void skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
  839. {
  840.     skb->sk = sk;
  841.     skb->destructor = sock_wfree;
  842.     atomic_add(skb->truesize, &sk->wmem_alloc);
  843. }
  844.  
  845. extern __inline__ void skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
  846. {
  847.     skb->sk = sk;
  848.     skb->destructor = sock_rfree;
  849.     atomic_add(skb->truesize, &sk->rmem_alloc);
  850. }
  851.  
  852.  
  853. extern __inline__ int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  854. {
  855. #ifdef CONFIG_FILTER
  856.     struct sk_filter *filter;
  857. #endif
  858.     /* Cast skb->rcvbuf to unsigned... It's pointless, but reduces
  859.        number of warnings when compiling with -W --ANK
  860.      */
  861.     if (atomic_read(&sk->rmem_alloc) + skb->truesize >= (unsigned)sk->rcvbuf)
  862.                 return -ENOMEM;
  863.  
  864. #ifdef CONFIG_FILTER
  865.     if ((filter = sk->filter) != NULL && sk_filter(skb, filter))
  866.         return -EPERM;    /* Toss packet */
  867. #endif /* CONFIG_FILTER */
  868.  
  869.     skb_set_owner_r(skb, sk);
  870.     skb_queue_tail(&sk->receive_queue, skb);
  871.     if (!sk->dead)
  872.         sk->data_ready(sk,skb->len);
  873.     return 0;
  874. }
  875.  
  876. extern __inline__ int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
  877. {
  878.     /* Cast skb->rcvbuf to unsigned... It's pointless, but reduces
  879.        number of warnings when compiling with -W --ANK
  880.      */
  881.     if (atomic_read(&sk->rmem_alloc) + skb->truesize >= (unsigned)sk->rcvbuf)
  882.         return -ENOMEM;
  883.     skb_set_owner_r(skb, sk);
  884.     skb_queue_tail(&sk->error_queue,skb);
  885.     if (!sk->dead)
  886.         sk->data_ready(sk,skb->len);
  887.     return 0;
  888. }
  889.  
  890. /*
  891.  *    Recover an error report and clear atomically
  892.  */
  893.  
  894. extern __inline__ int sock_error(struct sock *sk)
  895. {
  896.     int err=xchg(&sk->err,0);
  897.     return -err;
  898. }
  899.  
  900. extern __inline__ unsigned long sock_wspace(struct sock *sk)
  901. {
  902.     int amt = 0;
  903.  
  904.     if (!(sk->shutdown & SEND_SHUTDOWN)) {
  905.         amt = sk->sndbuf - atomic_read(&sk->wmem_alloc);
  906.         if (amt < 0) 
  907.             amt = 0;
  908.     }
  909.     return amt;
  910. }
  911.  
  912. /*
  913.  *    Default write policy as shown to user space via poll/select/SIGIO
  914.  *    Kernel internally doesn't use the MIN_WRITE_SPACE threshold.
  915.  */
  916. extern __inline__ int sock_writeable(struct sock *sk) 
  917. {
  918.     return sock_wspace(sk) >= MIN_WRITE_SPACE;
  919. }
  920.  
  921. /* 
  922.  *    Declarations from timer.c 
  923.  */
  924.  
  925. extern struct sock *timer_base;
  926.  
  927. extern void net_delete_timer (struct sock *);
  928. extern void net_reset_timer (struct sock *, int, unsigned long);
  929. extern void net_timer (unsigned long);
  930.  
  931. extern __inline__ int gfp_any(void)
  932. {
  933.     return in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
  934. }
  935.  
  936. /* 
  937.  *    Enable debug/info messages 
  938.  */
  939.  
  940. #if 1
  941. #define NETDEBUG(x)    do { } while (0)
  942. #else
  943. #define NETDEBUG(x)    do { x; } while (0)
  944. #endif
  945.  
  946. #endif    /* _SOCK_H */
  947.